package com.dewei.ling.ballsapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.dewei.ling.ballsapp.R;
import com.dewei.ling.ballsapp.activity.WebActivity1;
import com.dewei.ling.ballsapp.adapter.HotAdapter;
import com.dewei.ling.ballsapp.bean.HotData;
import com.dewei.ling.ballsapp.utils.L;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jet on 2018-10-10.
 */

public class HotFragment extends Fragment {
    private ListView mListView;
    private List<HotData> mList = new ArrayList<>();
    private List<String> mListTitle = new ArrayList<>();
    private List<String> mListUrl = new ArrayList<>();


    public static HotFragment newInstance(String name){
        Bundle args = new Bundle();
        args.putString("name", name);
        HotFragment fragment = new HotFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_hot,null);

        findView(view);
        return view;
    }

    private void findView(View view) {
        mListView = view.findViewById(R.id.mListView);
        //清理
        mList.clear();
        //获取当前系统时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String t1 = format.format(new Date());//2018-10-13
        //url
        String url = "https://s.qiumibao.com/json/record/"+ t1 +".htm?key=0.507066706487797";
        RxVolley.get(url, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                parsingJson(t);
                //Toast.makeText(getActivity(),t,Toast.LENGTH_SHORT).show();
                L.i("json:"+t);
            }
        });
        //点击
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                L.i("position:"+position);
                Intent intent = new Intent(getActivity(), WebActivity1.class);
                //2 way chuan zhi BUNdle

                intent.putExtra("title",mListTitle.get(position));
                intent.putExtra("url",mListUrl.get(position));
                startActivity(intent);
            }
        });

    }

    private void parsingJson(String t) {
        try{
            JSONObject jsonObject = new JSONObject(t);
            JSONArray jsonList = jsonObject.getJSONArray("list");

            for (int i = 0;i<jsonList.length();i++) {
                JSONObject json = (JSONObject) jsonList.get(i);
                HotData data = new HotData();

                String url = "https://news.zhibo8.cc" + json.getString("news_url");
                String title = json.getString("jijin_title");

                data.setJijin_title(json.getString("jijin_title"));
                data.setLabel(json.getString("label"));
                data.setTag(json.getString("tag"));
                data.setSdate(json.getString("sdate"));


                mListTitle.add(title);
                mList.add(data);
                mListUrl.add(url);

            }
            HotAdapter adapter = new HotAdapter(getActivity(),mList);
            mListView.setAdapter(adapter);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}
